package com.efei.jdbc.dynamic.query.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.efei.common.pojo.ResponseData;
import com.efei.common.tools.UnderlineToCamelUtil;
import com.efei.jdbc.dynamic.query.service.SysQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

public class SysQueryController {

    @Autowired
    private SysQueryService sysQueryService;

    @RequestMapping(path = "/sysquery/query/getPageList", method= {RequestMethod.POST,RequestMethod.GET})
    public ResponseData page(@RequestParam Long queryid, @RequestParam int page, @RequestParam int size, @RequestBody(required=false) String body){
        Pageable pageable = PageRequest.of(page, size);
        Page<?> data = sysQueryService.getPageList(queryid, pageable, body);
        return new ResponseData(data);
    }


    @RequestMapping(path = "/sysquery/query/getList", method= {RequestMethod.POST,RequestMethod.GET})
    public ResponseData list(@RequestParam Long queryid,@RequestBody(required=false) String body){
        Pageable pageable = Pageable.unpaged();
        Page<?> data = sysQueryService.getPageList(queryid, pageable, body);
        return new ResponseData(data.getContent());
    }


    @RequestMapping(path = "/sysquery/colum/getList", method=RequestMethod.GET)
    public ResponseData getColumList(@RequestParam Long queryid){
        return sysQueryService.getColumList(queryid);
    }



    /**
     * 开发使用
     * 查询列配置
     * @param queryid
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(path = "/sysquery/config/query/get", method=RequestMethod.GET)
    public ResponseData configQueryGet(@RequestParam Long queryid,@RequestParam String tbl,@RequestBody(required=false) String body){
        JSONArray ja = new JSONArray();
        Pageable pageable = PageRequest.of(0, 100000);
        Page<?> data = sysQueryService.getPageList(queryid, pageable, body);
        List<?> l = data.getContent();

        List<String> listA=Arrays.asList("company_id","create_by","create_time","update_time","","update_by","create_program","update_program");

        for(int i=0;i<l.size();i++) {
            Map<String,String> o = (Map<String, String>) l.get(i);
            String colname = o.get("colName").toLowerCase();
            if(listA.contains(colname)) {
                continue;
            }
            JSONObject jo = new JSONObject();
            jo.put("name", UnderlineToCamelUtil.underlineToCamel(o.get("colName")));
            jo.put("filed", o.get("colName").toLowerCase());
            jo.put("label", o.get("label"));
            jo.put("type", 0);
            ja.add(jo);

        }
        return new ResponseData(ja);
    }


//    /**
//     * 开发使用
//     * 表单配置
//     * @param queryid
//     * @return
//     */
//    @RequestMapping(path = "/sysquery/config/form/get", method=RequestMethod.GET)
//    public ResponseData configFormget(@RequestParam Long queryid,@RequestParam String tbl,@RequestBody(required=false) String body){
//        FormTableMap formTableMap = new FormTableMap();
//        List<TableMap> tableMapList = new ArrayList<TableMap>();
//        TableMap tableMap = new TableMap();
//        List<FiledMap> filedMap = new ArrayList<FiledMap>();
//
//        formTableMap.setTableMap(tableMapList);
//        tableMapList.add(tableMap);
//        tableMap.setFiledMap(filedMap);
//
//        tableMap.setTable(tbl.toLowerCase());
//        tableMap.setPkType("table");
//        tableMap.setPkFiled("xxxxx");
//        tableMap.setPkName("xxxxx");
//        tableMap.setPkGenerationSequence(tbl.toLowerCase());
//
//        Pageable pageable = PageRequest.of(0, 100000);
//        Page<?> data = sysQueryService.getPageList(queryid, pageable, body);
//        List<?> l = data.getContent();
//        List<String> listA=Arrays.asList("create_program","update_program");
//
//        for(int i=0;i<l.size();i++) {
//            Map<String,String> o = (Map<String, String>) l.get(i);
//            String colname = o.get("colName").toLowerCase();
//            if(listA.contains(colname)) {
//                continue;
//            }
//            FiledMap jo = new FiledMap();
//            jo.setName(UnderlineToCamelUtil.underlineToCamel(colname));
//            jo.setFiled(colname);
//            String type = o.get("dataType");
//            if(StringUtils.containsIgnoreCase(type, "number")) {
//                jo.setType("long");
//            } else if (StringUtils.containsIgnoreCase(type, "varchar")) {
//                jo.setType("string");
//            } else if (StringUtils.containsIgnoreCase(type, "date")) {
//                jo.setType("date");
//            } else {
//                jo.setType("");
//            }
//            if(StringUtils.equals("company_id", colname)) {
//                jo.setValue("{{companyid}}");
//            }
//            if(StringUtils.equals("create_by", colname)) {
//                jo.setUpdate(false);
//                jo.setValue("{{userid}}");
//            }
//            if(StringUtils.equals("create_time", colname)) {
//                jo.setUpdate(false);
//                jo.setType("time");
//                jo.setValue("{{time}}");
//            }
//            if(StringUtils.equals("update_by", colname)) {
//                jo.setValue("{{userid}}");
//            }
//            if(StringUtils.equals("update_time", colname)) {
//                jo.setType("time");
//                jo.setValue("{{time}}");
//            }
//            filedMap.add(jo);
//
//        }
//
//        String json = JSON.toJSONString(formTableMap,true);
//
//        return new ResponseData(JSON.parseObject(json,Feature.OrderedField));
//    }
}

